﻿using DBUtil.FastEntity.Annotations;
using DotNetCommon.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;

namespace DBUtil.FastEntity.Interface
{
    #region 表查询: 单表
    /// <summary>
    /// 表示Select子句
    /// 查询执行的流程: 指定表列->条件->排序->执行
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface ISelectClause<T> where T : class
    {
        #region 构造

        #region 过滤
        /// <summary>
        /// 过滤条件
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        ISelectClause<T> Where(Expression<Func<T, bool>> expression);

        /// <summary>
        /// 根据主键过滤
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        ISelectClause<T> WhereKey(params int[] key);

        /// <summary>
        /// 根据主键过滤
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        ISelectClause<T> WhereKey(params long[] key);

        /// <summary>
        /// 根据主键过滤
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        ISelectClause<T> WhereKey(params string[] key);
        #endregion

        #region 分组
        ///// <summary>
        ///// 分组
        ///// </summary>
        ///// <typeparam name="TKey"></typeparam>
        ///// <param name="expression"></param>
        ///// <returns></returns>
        //IGroupByClause<TKey, T> GroupBy<TKey>(Expression<Func<T, TKey>> expression);
        #endregion

        #region 分页排序
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        ISelectClause<T> OrderBy(Expression<Func<T, object>> expression);

        /// <summary>
        /// 排序(倒序)
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        ISelectClause<T> OrderByDesc(Expression<Func<T, object>> expression);
        #endregion

        #region 引入关联字段和关联表

        /// <summary>
        /// 指定一个或多个扩展列<para></para>
        /// <code>
        /// 示例：<br />
        /// <para>.IncludeExtendColumn(t => t.Column1)</para>
        /// .IncludeExtendColumn(t => new { t.Column1,t.Column2 })
        /// </code>
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        ISelectClause<T> IncludeExtendColumn(Expression<Func<T, object>> expression);

        /// <summary>
        /// 指定一个或多个扩展列<para></para>
        /// <code>
        /// 示例：<br />
        /// <para>.IncludeExtendColumn(t => t.Table1)</para>
        /// .IncludeExtendColumn(t => new { t.Table1,t.Table1 })
        /// </code>
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        ISelectClause<T> IncludeExtendTable(Expression<Func<T, object>> expression);

        #endregion

        #endregion

        #region 执行
        /// <summary>
        /// 返回查到的第一个对象,如果没查到就返回null
        /// </summary>
        /// <returns></returns>
        T FirstOrDefault();

        /// <summary>
        /// 返回查询到的结果
        /// </summary>
        /// <returns></returns>
        List<T> ToList();

        /// <summary>
        /// 返回生成的Sql
        /// </summary>
        /// <returns></returns>
        string ToSql();

        /// <summary>
        /// 返回生成的Sql和参数化数组
        /// </summary>
        /// <returns></returns>
        (string sql, IDataParameter[] paras) ToSqlWithParas();

        /// <summary>
        /// 执行查询,返回分页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        Page<T> ToPage(int pageIndex = 1, int pageSize = 10);

        /// <summary>
        /// 执行查询，返回记录数
        /// </summary>
        /// <returns></returns>
        long Count();

        /// <summary>
        /// 获取单列和
        /// </summary>
        /// <typeparam name="TProp"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        TProp Sum<TProp>(Expression<Func<T, TProp>> expression);

        /// <summary>
        /// 获取单列最大值
        /// </summary>
        /// <typeparam name="TProp"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        TProp Max<TProp>(Expression<Func<T, TProp>> expression);

        /// <summary>
        /// 获取单列最小值
        /// </summary>
        /// <typeparam name="TProp"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        TProp Min<TProp>(Expression<Func<T, TProp>> expression);

        /// <summary>
        /// 获取单列平均值
        /// </summary>
        /// <typeparam name="TProp"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        TProp Avg<TProp>(Expression<Func<T, TProp>> expression);
        #endregion

    }
    #endregion
}
